/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.util.ArrayList;
import model.Locatie;

/**
 *
 * @author misja
 */
public class LocatieDialoog extends javax.swing.JFrame {

    /**
     * Creates new form LocatieDialoog
     */
    private char actieCode;
    private String huisnummer;
    private String postcode;
    private Locatie clsLocatie;

    public LocatieDialoog(char argActieCode, String argHuisnummer, String argPostcode) {
        initComponents();

        if (!Database.openConnection("fullhouse", "root", "")) {
            System.out.println("Geen verbinding met de database");
            System.exit(0);
        }

        this.actieCode = argActieCode;
        this.postcode = argPostcode;
        this.huisnummer = argHuisnummer;




        if (this.postcode.isEmpty() || this.huisnummer.isEmpty()) {
            Locatie locatie = new Locatie();
            this.clsLocatie = locatie;
        } else {
            Locatie locatie = new Locatie(this.huisnummer, this.postcode);
            this.clsLocatie = locatie;
        }

        txtNaam.setText(clsLocatie.getNaam());
        txtPlaats.setText(clsLocatie.getPlaats());
        txtStraat.setText(clsLocatie.getStraat());
        txtHuisnummer.setText(clsLocatie.getHuisnummer());
        txtPostcode.setText(clsLocatie.getPostcode());
        txtDeelnemers.setText(Integer.toString(clsLocatie.getDeelnemers()));

        switch (this.actieCode) {
            case 't':
                btnOke.setText("Toevoegen");
                break;
            case 'b':
                btnOke.setText("Bijwerken");
                break;
            case 'v':
                btnOke.setText("Verwijderen");
                txtNaam.setEditable(false);
                txtPlaats.setEditable(false);
                txtStraat.setEditable(false);
                txtHuisnummer.setEditable(false);
                txtPostcode.setEditable(false);
                txtDeelnemers.setEditable(false);
                break;
        }
    }

    LocatieDialoog() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNaam = new javax.swing.JLabel();
        lblPlaats = new javax.swing.JLabel();
        lblStraat = new javax.swing.JLabel();
        lblHuisnummer = new javax.swing.JLabel();
        lblPostcode = new javax.swing.JLabel();
        lblDeelnemers = new javax.swing.JLabel();
        txtNaam = new javax.swing.JTextField();
        txtPlaats = new javax.swing.JTextField();
        txtStraat = new javax.swing.JTextField();
        txtHuisnummer = new javax.swing.JTextField();
        txtPostcode = new javax.swing.JTextField();
        txtDeelnemers = new javax.swing.JTextField();
        btnOke = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txteBericht = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 500));
        setName("Locatie"); // NOI18N
        getContentPane().setLayout(null);

        lblNaam.setText("naam ");
        getContentPane().add(lblNaam);
        lblNaam.setBounds(10, 22, 60, 14);

        lblPlaats.setText("plaats ");
        getContentPane().add(lblPlaats);
        lblPlaats.setBounds(10, 54, 60, 14);

        lblStraat.setText("straat ");
        getContentPane().add(lblStraat);
        lblStraat.setBounds(10, 92, 60, 14);

        lblHuisnummer.setText("huisnummer ");
        getContentPane().add(lblHuisnummer);
        lblHuisnummer.setBounds(10, 150, 80, 14);

        lblPostcode.setText("postcode ");
        getContentPane().add(lblPostcode);
        lblPostcode.setBounds(10, 180, 70, 14);

        lblDeelnemers.setText("deelnemers ");
        getContentPane().add(lblDeelnemers);
        lblDeelnemers.setBounds(10, 210, 90, 14);

        txtNaam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNaamActionPerformed(evt);
            }
        });
        getContentPane().add(txtNaam);
        txtNaam.setBounds(130, 20, 190, 30);
        getContentPane().add(txtPlaats);
        txtPlaats.setBounds(130, 50, 190, 30);
        getContentPane().add(txtStraat);
        txtStraat.setBounds(130, 80, 74, 30);

        txtHuisnummer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHuisnummerActionPerformed(evt);
            }
        });
        getContentPane().add(txtHuisnummer);
        txtHuisnummer.setBounds(130, 140, 190, 30);
        getContentPane().add(txtPostcode);
        txtPostcode.setBounds(130, 170, 190, 30);

        txtDeelnemers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDeelnemersActionPerformed(evt);
            }
        });
        getContentPane().add(txtDeelnemers);
        txtDeelnemers.setBounds(130, 200, 190, 30);

        btnOke.setText("Toevoegen");
        btnOke.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkeActionPerformed(evt);
            }
        });
        getContentPane().add(btnOke);
        btnOke.setBounds(40, 270, 120, 23);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancel);
        btnCancel.setBounds(190, 270, 110, 23);

        txteBericht.setColumns(20);
        txteBericht.setRows(5);
        jScrollPane1.setViewportView(txteBericht);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 317, 341, 96);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNaamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNaamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNaamActionPerformed

    private void btnOkeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkeActionPerformed

        clsLocatie.setNaam(txtNaam.getText());
        clsLocatie.setPlaats(txtPlaats.getText());
        clsLocatie.setStraat(txtStraat.getText());
        clsLocatie.setHuisnummer(txtHuisnummer.getText());
        clsLocatie.setPostcode(txtPostcode.getText());
        clsLocatie.setDeelnemers(txtDeelnemers.getText());

        switch (actieCode) {
            case 't':
                clsLocatie.toevoegen();
                break;
            case 'b':
                clsLocatie.bijwerken(this.huisnummer, this.postcode);
                break;
            case 'v':
                clsLocatie.verwijderen();
                break;
        }

        if (clsLocatie.isGevalideerd()) {
            this.setVisible(false);
            new LocatieIndex().setVisible(true);
        } else {
            String bericht = "";
            ArrayList berichten = clsLocatie.getBericht();
            for (int i = 0; i < berichten.size(); i++) {
                bericht = bericht + (String) berichten.get(i);
            }
            txteBericht.setText(bericht);
        }
    }//GEN-LAST:event_btnOkeActionPerformed

    private void txtHuisnummerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHuisnummerActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHuisnummerActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    }//GEN-LAST:event_btnCancelActionPerformed

    private void txtDeelnemersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDeelnemersActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDeelnemersActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LocatieDialoog('t', "", "").setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOke;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDeelnemers;
    private javax.swing.JLabel lblHuisnummer;
    private javax.swing.JLabel lblNaam;
    private javax.swing.JLabel lblPlaats;
    private javax.swing.JLabel lblPostcode;
    private javax.swing.JLabel lblStraat;
    private javax.swing.JTextField txtDeelnemers;
    private javax.swing.JTextField txtHuisnummer;
    private javax.swing.JTextField txtNaam;
    private javax.swing.JTextField txtPlaats;
    private javax.swing.JTextField txtPostcode;
    private javax.swing.JTextField txtStraat;
    private javax.swing.JTextArea txteBericht;
    // End of variables declaration//GEN-END:variables
}
